﻿using Deployment.Data.Deploy.Steps.Tasks;
using Deployment.Data.Environment.Connection;
using Deployment.Data.SQL.Connections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.SQL.Tasks.UI
{
    /// <summary>
    /// Interaction logic for SQLScriptTaskControl.xaml
    /// </summary>
    public partial class SQLScriptTaskControl : AbstractTaskControl
    {
        //public static readonly DependencyProperty AvailableConnectionNamesProperty =
        //    DependencyProperty.Register("AvailableConnectionNames",
        //        typeof(List<string>),
        //        typeof(SQLScriptTaskControl), null);

        public SQLScriptTaskControl()
        {
            InitializeComponent();
            this.DataContext = new SQLScriptTask();
        }

        //public List<string> AvailableConnectionNames
        //{
        //    get { return (List<string>)GetValue(AvailableConnectionNamesProperty); }
        //    set { SetValueDp(AvailableConnectionNamesProperty, value); }
        //}

        protected override void process_EvnvironmentsFileChanged()
        {
            List<string> connections = new List<string>();
            if (EnvironmentsFile != null)
            {
                NameEquality eq = new NameEquality();
                foreach (Environment.Environment env in EnvironmentsFile.Environments)
                {
                    List<AbstractConnection> availableConnections = env.getConnectionsByType(typeof(AbstractSQLConnection));
                    foreach (AbstractConnection con in availableConnections)
                    {
                        if (!connections.Contains(con.Name, eq))
                            connections.Add(con.Name);
                    }
                }
                connections.Sort();
            }
            this.ConnectionCombo.ItemsSource = connections;
        }

        private class NameEquality : IEqualityComparer<string>
        {

            public bool Equals(string x, string y)
            {
                return string.Equals(x, y, StringComparison.CurrentCultureIgnoreCase);
            }

            public int GetHashCode(string obj)
            {
                return obj.GetHashCode();
            }
        }

        private void Browse_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
